package com.example.brazil_project.function;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.example.brazil_project.AddProductActivity;
import com.example.brazil_project.DucumentActivity;
import com.example.brazil_project.DucumentDetailActivity;
import com.example.brazil_project.RedactDocumentDetailActivity;
import com.example.brazil_project.SearchProductActivity;
import com.example.brazil_project.parameter.Parameter;
import com.example.brazil_project.util.HttpUtil;
import com.example.brazil_project.util.IOUtil;
import com.example.brazil_project.util.ImageCutUtil;
import com.example.brazil_project.util.JSFunction;
import com.example.brazil_project.util.ParameterUtil;
import com.example.brazil_project.util.StringUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.webkit.WebView;

public class DucumentFunction {
	Activity mContxt;  
	WebView webview;
	 public DucumentFunction(Activity mContxt,Object webview) {  
	        this.mContxt = mContxt;  
	        this.webview = (WebView) webview;
	  }  
	 //跳转到产品详情
	 public String goDud(String du_id){
		 Intent in = mContxt.getIntent();
		 String username = in.getStringExtra("username");
		 Intent intent = new Intent(mContxt, DucumentDetailActivity.class);
		 if(username==null||username.length()<=0){
			 Public_method p = new Public_method(mContxt);
			 username =  p.getLoginUserName();
			
		 }
		 intent.putExtra("du_id", du_id);
		 intent.putExtra("username", username);
		 mContxt.startActivity(intent);
		 
		 return "1";
	 }
	 
	 //根据产品ID获取产品
	 public String getProductbyId(){
		 String du_id= mContxt.getIntent().getStringExtra("du_id");
		 String url = Parameter.getServerPath()+"APP/Product/getProductbyId.nut?id="+du_id;
		 String  msg = HttpUtil.doPost(url);
		 return msg;
	 }
	
	 
	 /**
	  * 根据用户名查找产品
	  * @return
	  */
	 public String getProductbyUserid(){
		 Intent intent = mContxt.getIntent();
		 String username = intent.getStringExtra("username");
		 String keyword = intent.getStringExtra("keyword");
		//Public_method pum = new Public_method(mContxt);
		//String username =  pum.getLoginUserName();
		String url = Parameter.getServerPath()+"APP/Product/getProductbyUserid.nut";
		Map map = new HashMap();
		map.put("username", username);
		map.put("keyword", keyword);
		String  msg = HttpUtil.doPost(url,map);
		return msg;
		 
	 }
	 
	 /**
	  * 根据用户名查找产品
	  * @return
	  */
	 public String getProductbyUserid(String username){
		//Public_method pum = new Public_method(mContxt);
		//String username =  pum.getLoginUserName();
		String url = Parameter.getServerPath()+"APP/Product/getProductbyUserid.nut";
		
		Intent intent = mContxt.getIntent();
		String keyword = intent.getStringExtra("keword");
		Map map = new HashMap();
		map.put("username", username);
		map.put("keyword", keyword);
		String  msg = HttpUtil.doPost(url,map);
		
		return msg;
		 
	 }
	 
	/**
	 * 跳转到产品编辑页面
	 * @param id 产品ID
	 */
	 public void gotoRedactDocument(String id){
		 if(id==null||id.equals("")){
			 id = 0+"";
		 }
		 Intent intent = new Intent(mContxt, RedactDocumentDetailActivity.class);
		 intent.putExtra("Product_id",id);
		 mContxt.startActivity(intent);
	 }
	 
	 
	/**AddProductActivity
	 * 跳转到产品编辑页面
	 * @param id 产品ID
	 */
	 public void gotoAddProduct(){
	
		 Intent intent = new Intent(mContxt, AddProductActivity.class);

		 mContxt.startActivity(intent);
	 }
	 
	 /**
	  * 根据父ID 获取数据库表产品的所有子类型
	  * @return
	  */
	 public String getProductType(String pid){
		String url = Parameter.getServerPath()+"APP/Product/getProductType.nut?pid="+pid;
		String  msg = HttpUtil.doPost(url);
		return msg;
		 
	 }
	 
	 /**
	  * 根据ID 获取数据库表产品的类型
	  * @return
	  */
	 public String getProductTypebyId(String id){
		String url = Parameter.getServerPath()+"APP/Product/getProductTypebyId.nut?id="+id;
		String  msg = HttpUtil.doPost(url);
		return msg;
		 
	 }
	 /**
	  * 根据类型获取所有的子父级
	  * @return
	  */
	 public String getProductAllType(String id){
		 
			String url = Parameter.getServerPath()+"APP/Product/getProductAllType.nut?id="+id;
			String  msg = HttpUtil.doPost(url);
			return msg;
		 
	 }
	 
	 /**
	  * 根据ID删除产品
	  * @param id
	  * @return
	  */
	 public String deleteProductbyId(String id){
		String url = Parameter.getServerPath()+"APP/Product/deleteProductbyId.nut?id="+id;
		String  msg = HttpUtil.doPost(url);
		webview.loadUrl(JSFunction.getAlertMSGFunction(msg)); 
		mContxt.finish();
		return msg;
		 
	 }
	 
	 
	 /**
	  * 添加一个产品
	  * @param data
	  * @return
	  */
	 public String addProduct(String data){
		 Map map = ParameterUtil.getParameter(data);
		 String input_img_1 = map.get("input_img_1")+"";
		 String input_img_2 = map.get("input_img_2")+"";
		 String input_img_3 = map.get("input_img_3")+"";
		 List<NameValuePair> params = new ArrayList<NameValuePair>();
		 if(!StringUtils.isBlank(input_img_1)){
			 String FiletoString =IOUtil.FiletoString(input_img_1,true);
			 params.add(new BasicNameValuePair("img_1_Stream",FiletoString));  
			 map.put("img_1_Stream", FiletoString);
		 }
		 if(!StringUtils.isBlank(input_img_2)){
			 String FiletoString =IOUtil.FiletoString(input_img_2,true);
			 params.add(new BasicNameValuePair("img_2_Stream",FiletoString));
			 map.put("img_2_Stream", FiletoString);
		 }
		 
		 if(!StringUtils.isBlank(input_img_3)){
			 String FiletoString =IOUtil.FiletoString(input_img_3,true);
			 params.add(new BasicNameValuePair("img_3_Stream",FiletoString));  
			 map.put("img_3_Stream", FiletoString);
		 }
		 
		
		 
//		String url = Parameter.getServerPath()+"APP/Product/addProduct.nut?"+data;
//		String  msg = HttpUtil.doPost(url,params);
		 String url = Parameter.getServerPath()+"APP/Product/addProduct.nut";
		 String  msg = HttpUtil.doPost(url,map);
	 
	 	
		webview.loadUrl(JSFunction.getAlertMSGFunction(msg)); 		 
	 return msg; 
	
		 
	 }
	 
	 /**
	  * 产品修改
	  * @param data
	  * @return
	  */
	 public String updateProduct(String data){
		 Map map = ParameterUtil.getParameter(data);
		 String input_img_1 =  map.get("input_img_1")+"";
		 String input_img_2 =  map.get("input_img_2")+"";
		 String input_img_3 =  map.get("input_img_3")+"";
		 List<NameValuePair> params = new ArrayList<NameValuePair>();
		 if(!StringUtils.isBlank(input_img_1)){
			
			 String FiletoString =IOUtil.FiletoString(input_img_1,true);
			 params.add(new BasicNameValuePair("img_1_Stream",FiletoString)); 
			 boolean isimg =false;
			 isimg= input_img_1.startsWith("Product/");
			 if(isimg){
				 FiletoString = "true";
			 }
			 map.put("img_1_Stream", FiletoString);
		 }
		 if(!StringUtils.isBlank(input_img_2)){
			 String FiletoString =IOUtil.FiletoString(input_img_2,true);
			 params.add(new BasicNameValuePair("img_2_Stream",FiletoString));
			 boolean isimg =false;
			 isimg= input_img_2.startsWith("Product/");
			 if(isimg){
				 FiletoString = "true";
			 }
			 map.put("img_2_Stream", FiletoString);
		 }
		 
		 if(!StringUtils.isBlank(input_img_3)){
			 String FiletoString =IOUtil.FiletoString(input_img_3,true);
			 params.add(new BasicNameValuePair("img_3_Stream",FiletoString));  
			 boolean isimg =false;
			 isimg= input_img_3.startsWith("Product/");
			 if(isimg){
				 FiletoString = "true";
			 }
			 map.put("img_3_Stream", FiletoString);
		 }

		 String url = Parameter.getServerPath()+"APP/Product/updateProduct.nut";
		 String  msg = HttpUtil.doPost(url,map);
			
		webview.loadUrl(JSFunction.getAlertMSGFunction(msg)); 		 
		 return msg; 
		
		 
	 }
	 
	 //跳转到产品搜索
	 public void gotoSearchProduct(String keyword,String page,String oredr){
		 Intent intent = new Intent(mContxt, SearchProductActivity.class);
		 intent.putExtra("keyword", keyword);
		 intent.putExtra("page", page);
		 intent.putExtra("order", oredr);
		 
		 if(keyword!=null){
			 keyword = keyword.replace(" ","");
			 keyword = keyword.replace("undefined","");
			 if(keyword==""||keyword.length()<=0){
				 webview.loadUrl(JSFunction.getAlertMSGFunction("请输入关键字"));
			 }
			 else{
				 mContxt.startActivity(intent);
			 }
		 }else{
			 webview.loadUrl(JSFunction.getAlertMSGFunction("请输入关键字"));
		 }
		
		 
	 }
	 //添加我的产品库
	 public void addmywarehouse(String id){
		 Map map = new HashMap();
		 String url = Parameter.getServerPath()+"APP/Product/addmywarehouse.nut";
		 map.put("p_id", id);
		 map.put("username", new Public_method(mContxt).getLoginUserName());
		 String  msg = HttpUtil.doPost(url,map);
		 webview.loadUrl(JSFunction.getAlertMSGFunction(msg));
	 }
	 
}
